/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.servlets;

//DW/2621/BeginPatch
import opiam.admin.faare.MessageUtil;
//DW/2621/EndPatch
import opiam.admin.faare.service.services.trombinoscope.TrombinoscopeGenerator;

import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * This servlet sends a PDF trombinoscope as attached PDF file.<br>
 * HTTP PARAMETERS :<br>
 * <li>entries : name of a session attribute containing the list of entries</li>
 * <li>attribs : name of a session attribute containing the list of attributes</li>
 * <li>start : start index</li>
 * <li>nb : number of entries</li>
 * <li>cols : number of columns</li>
 *<br>
 * WARNING : do not forget to configure the servlet in web.xml.
 */
public class TrombiDispatcherServlet extends HttpServlet
{
    /** Log4j logger. */
    private static Logger _logger = Logger.getLogger(TrombiDispatcherServlet.class);

    /**
     * Servlet processing method.
     * See HttpServlet.doGet
     *
     * @param request HTTP request
     * @param response HTTP response
     *
     * @throws java.io.IOException see HttpServlet
     * @throws javax.servlet.ServletException see HttpServlet
     */
    @Override
	public final void doGet(final HttpServletRequest request,
        final HttpServletResponse response)
        throws IOException, ServletException
    {
        String startS = request.getParameter("start");
        String colsS = request.getParameter("cols");
        String nbS = request.getParameter("nb");
        String entriesS = request.getParameter("entries");
        String attribsS = request.getParameter("attribs");

        _logger.debug("start = " + startS);
        _logger.debug("cols = " + colsS);
        _logger.debug("nb = " + nbS);
        _logger.debug("entries = " + entriesS);
        _logger.debug("attribs = " + attribsS);
//DW/2621/BeginPatch
        if ((entriesS == null) || entriesS.trim().equals(""))
        {
            throw new ServletException(MessageUtil.getMessageString("MSG_ARGUMENT_NULL"));
        }
        if ((attribsS == null) || attribsS.trim().equals(""))
        {
            throw new ServletException(MessageUtil.getMessageString("MSG_ARGUMENT_NULL"));
        }
        if ((startS == null) || startS.trim().equals(""))
        {
            startS = "0";
        }
        if ((colsS == null) || colsS.trim().equals(""))
        {
            colsS = "5";
        }
//DW/2621/EndPatch

        try
        {
            HttpSession session = request.getSession();
            int start = Integer.parseInt(startS);
            int cols = Integer.parseInt(colsS);
            List entries = (List) session.getAttribute(entriesS);
            List attribs = (List) session.getAttribute(attribsS);
//DW/2621/BeginPatch
            int nb;
            if ((nbS == null) || nbS.trim().equals(""))
            {
                nb = entries.size();
            }
            else
            {
                nb = Integer.parseInt(nbS);
            }
//DW/2621/EndPatch

            ByteArrayOutputStream output = TrombinoscopeGenerator.generatePdfTrombinoscope(start,
                    cols, nb, entries, attribs);

            byte[] byteArray = output.toByteArray();
            _logger.debug("byteArray length : " + byteArray.length);
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition",
                "attachment; filename=trombi.pdf"); //DW/2531

            ServletOutputStream servletOutputStream = response.getOutputStream();

            servletOutputStream.write(byteArray, 0, byteArray.length);
            servletOutputStream.flush();

            // Flush and close streams
            servletOutputStream.close();
        }
        catch (Exception se)
        {
            se.printStackTrace();
        }
    }
}
